<?php
namespace app\wxmp\controller;
use x\my\Pay;

class Admin extends Base{
    public function __construct(){
        parent::__construct();
        $this->checkLogin();
        $end_time = m('Userxiangmu','c')->check($this->userinfo['id'], 2);
        if(!$end_time){
          r(url('bbs/userxiangmu/pay',['id'=>2]));
        }
        $this->assign('page_title','微信名片');
    }
    //首页
    public function index(){
      $this->fetch([],'','layadmin');
    }

    //列表
    public function lists() {
        if (is_post()) {
            $d = $this->post;
            $map = [];
            $map[] = ['and','user_id', '=',$this->userinfo['id']];
            if ($d['keyword'] != '') {
                $map[] = ['and', 'title|name', 'like', "%{$d['keyword']}%"];
            }
            $w = where($map);
            $rs = $this->db->query("select * from wxmp $w[0] order by id asc" . limit($d['page'], $d['limit']), $w[1], '', true);
            if ($rs) {
                foreach ($rs[0] as $key => $val) {
                    $rs[0][$key]['create_time'] = date('Y-m-d H:i', $val['create_time']);
                }
                suc($rs[0], $rs[1]);
            } else {
                suc([], 0);
            }
        }
        $this->fetch([],'','layadmin');
    }

    //添加
    public function add(){
      if(is_post()){
        $d = $this->post;
        $rule = [
          'pic'=>'require',
          // 'pic_wx'=>'require',
          // 'lat_lng'=>'require',
          'phone'=>'require|mobile',
        ];
        $msg = [
          'pic.require' => '请上传头像',
          //'pic_wx.require' => '请上传微信二维码',
          'phone.mobile' => '手机号码格式不正确',
          //'lat_lng.require' => '请粘贴百度经纬度',
        ];
        check($rule,$msg,$d);
        if(isset($d['file'])) unset($d['file']);
        //相册图
        if(isset($d['pics'])){
            $d['pics'] = implode(",",$d["pics"]);
        }else{
            $d['pics'] = '';
        }
        //验证经纬度
        $p = "/^((-?(([1-9]?[0-9])|(1[0-7][0-9]))([.])?\d*)|(180))[,]((-?(([1-8]?[0-9])|)([.])?\d*)|(90))$/";
        $rs = preg_match($p, $d['lat_lng']);
        $arr = explode(",", $d['lat_lng']);

        unset($d['lat_lng']);
        $d['lat'] = isset($arr[0]) ? $arr[0] : 0;
        $d['lng'] = isset($arr[1]) ? $arr[1] : 0;

        $d['create_time'] = time();
        $d['user_id'] = $this->userinfo['id'];
        $id = $this->db->insert('wxmp',$d);
        m('Attachment','xcom')->imgSure('wxmp',$id,$d);
        if($id){
          cookie('js_msg','添加成功');
          cookie('js_function','preload');
          suc();
        }else{
          err('添加失败');
        }
      }
      $this->fetch([],'','layno');
    }

    //编辑
    public function edit(){
      if(is_post()){
        $d = $this->post;
        $rule = [
          'pic'=>'require',
          //'pic_wx'=>'require',
          //'lat_lng'=>'require',
          'phone'=>'require|mobile',
        ];
        $msg = [
          'pic.require' => '请上传头像',
          //'pic_wx.require' => '请上传微信二维码',
          'phone.mobile' => '手机号码格式不正确',
          //'lat_lng.require' => '请粘贴百度经纬度',
        ];
        check($rule,$msg,$d);
        if(isset($d['file'])) unset($d['file']);
        //相册图
        if(isset($d['pics'])){
            $d['pics'] = implode(",",$d["pics"]);
        }else{
            $d['pics'] = '';
        }
        //验证经纬度
        $p = "/^((-?(([1-9]?[0-9])|(1[0-7][0-9]))([.])?\d*)|(180))[,]((-?(([1-8]?[0-9])|)([.])?\d*)|(90))$/";
        $rs = preg_match($p, $d['lat_lng']);
        $arr = explode(",", $d['lat_lng']);

        unset($d['lat_lng']);
        $d['lat'] = isset($arr[0]) ? $arr[0] : 0;
        $d['lng'] = isset($arr[1]) ? $arr[1] : 0;

        $d['update_time'] = time();
        $this->db->update('wxmp',$d);
        m('Attachment','xcom')->imgSure('wxmp',$d['id'],$d);
        cookie('js_msg','修改成功');
        cookie('js_function','preload');
        suc();
      }
      $data = [
        'info' => $this->db->find('wxmp',$this->get('id'))
      ];
      $this->fetch($data,'','layno');
    }

    //详情
    public function detail(){
      $txt = urlencode(url('wxmp/index/index',['id'=>$this->get('id')]));
      $img_url = url('bbs/com/qrcode',['txt'=>$txt]);
      $this->fetch(['img_url'=>$img_url],'','layno');
    }
    //删除
    public function del(){
        if(is_post()){
            $d = $this->post;
            $rs = $this->db->find('wxmp',$d['id']);
            m('Wxmedia','c')->del("./uploads/user/{$rs['user_id']}/wxmp_{$d['id']}.png");
            $this->db->delete('wxmp',$d['id']);
            m('Attachment','xcom')->imgDel('wxmp',$d['id'],true);
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }
    }
}
?>
